var comPageNum = 0;//分页公共页码
var isEdit = false;//是否编辑--判断是否跟新页码到首页
var num = [];
$("#form-member").validate({
    rules: {
        category_id: {
            maxlength:10,
            minlength:10,
        },
        logmax: {
            maxlength:30,
        },
        logmin: {
            maxlength:10,
            minlength:10,
        },
        shaixuan: {
            maxlength:10,
            minlength:10,
        }
    },

    //提交表单数据
    submitHandler:function(form){
        queryDataFn();
    }
})
function queryDataFn() {
    //获取
    status=$("#shaixuan").val();
    pinlei=$("#pinlei").find("option:selected").val();

    var user_data = {
        useStatus:"",
        categoryName:pinlei
    }

    //获取返回的数据
    var succCallBack = function (data, status, response,i) {
        console.log(data)
        if(data.errorCode=="0") {

            var dataSource = new Array();
            var table;   //table对象
            dataSource = [];
            var result=data.data

            $.each(result, function (i, field){
                var numArr = [];
                var rowData = new Array();
                //活动标题-0
                rowData.push("品类折扣价");

                rowData.push(field.category_name);
                //价格基数-1
                rowData.push("批发价");
                //折扣率-2
                if (field.value==undefined){
                    var a=undefined;
                    var b=undefined;
                    var c=undefined;
                    var d=undefined;
                    var e=undefined;
                    var f=undefined;
                    var g=undefined;
                    var h=undefined;
                    var i=undefined;
                    var j=undefined;
                }else{
                    // var tttt=field.value.split(",")
                    // var pinlei=tttt.reverse();
                    var pinlei=field.value.split(",")
                    var a=pinlei[0];
                    var b=pinlei[1];
                    var c=pinlei[2];
                    var d=pinlei[3];
                    var e=pinlei[4];
                    var f=pinlei[5];
                    var g=pinlei[6];
                    var h=pinlei[7];
                    var i=pinlei[8];
                    var j=pinlei[9];
                }
                if(!a==""){
                    $('#a').show();
                    rowData.push(a+"%");
                    numArr.push(a);
                }else if(a==undefined){
                    rowData.push("");
                }
                if(!b==""){
                    $('#b').show();
                    rowData.push(b+"%");
                    numArr.push(b);
                }else if(b==undefined){
                    rowData.push("");
                }
                if(!c==""){
                    $('#c').show();
                    rowData.push(c+"%");
                    numArr.push(c);
                }else if(c==undefined){
                    rowData.push("");
                }
                if(!d==""){
                    $('#d').show();
                    rowData.push(d+"%");
                    numArr.push(d);
                }else if(d==undefined){
                    rowData.push("");
                }
                if(!e==""){
                    $('#e').show();
                    rowData.push(e+"%");
                    numArr.push(e);
                }else if(e==undefined){
                    rowData.push("");
                }
                if(!f==""){
                    $('#f').show();
                    rowData.push(f+"%");
                    numArr.push(f);
                }else if(f==undefined){
                    rowData.push("");
                }
                if(!g==""){
                    $('#g').show();
                    rowData.push(g+"%");
                    numArr.push(g);
                }else if(g==undefined){
                    rowData.push("");
                }
                if(!h==""){
                    $('#h').show();
                    rowData.push(h+"%");
                    numArr.push(h);
                }else if(h==undefined){
                    rowData.push("");
                }
                if(!i==""){
                    $('#i').show();
                    rowData.push(i+"%");
                    numArr.push(i);
                }else if(i==undefined){
                    rowData.push("");
                }
                if(!j==""){
                    $('#j').show();
                    rowData.push(j+"%");
                    numArr.push(j);
                }else if(j==undefined){
                    rowData.push("");
                }
                //状态 4
                rowData.push(field.UseStatus);
                //创建人-5
                rowData.push(field.creation_by);
                //更新时间-6
                rowData.push(field.last_modified_date);
                //7 品类id
                numArr.push(field.category_name);
                //8
                numArr.push(field.discount_id);
                dataSource.push(rowData);
                rowData.push(numArr);
            });
            initDataTable(dataSource);
            return false
        }else{
            var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            return false
        }
    };
    //给一个接口地址提交数据
    return web.doAjax(root.sroreinter.getDiscount, 'post', user_data, succCallBack);
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable

        $("#datatable").DataTable({
            "fnDrawCallback" : function(){
                if(!isEdit){
                    comPageNum = getDataTablePageNum(dttable);
                }
                //权限控制函数
                power()
            },
            "autoWidth": false,
            "order": [[ 15, "desc" ]],
            "ordering": true,
            "searching": true,
            "paging": true,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data,type,row,meta){
                        if(row[13]=="1"){
                            var val='<span>生效</span>'
                        }else {
                            var val='<span>请联系管理员</span>'
                        }
                        return val;
                    },
                    "targets": 13
                },
                {
                    "render": function(data,type,row,met){
                        var val ='<a class="powerButton 203"><i class="Hui-iconfont" onclick="edit_data(\''+row[16]+'\')">编辑</i></a>';
                        return val;
                    },
                    "targets": 16
                },
            ]
        });
        dttable.fnPageChange(comPageNum);
        isEdit = false;
        //权限控制函数
        power()
    };
}
/*修改*/
function edit_data(num){
    var index = layer.open({
        type: 2,
        title:"商品运营编辑配置",
        content: "discount_edit.html"+"?numArr="+num
    });
    layer.full(index);
}
/*
下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.goodsInter.goods_listCategory,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_level:1
        },
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#pinlei").html(interText(dataInter));

            }
        }
    })
})
/*
下拉框的展示
*/
// $(function () {
//     $.ajax({
//         type: "post",
//         url: "http://192.168.0.53:8380/operation/get_category.jhtml",
//         cache: false,  //禁用缓存
//         dataType: "json",
//         data: {category_id:19,type:101},  //传入组装的参数
//         success: function (data) {
//             if(data.errorCode=="0") {
//                 var dataInter=data.data;
//                 var interText = doT.template($("#ascription_bob").text());
//                 $("#category_id").html(interText(dataInter));
//             }
//             $.each(dataInter,function(inx,data2){
//                 if(data2.name=="品类折扣价配置"){
//                     var member='<option selected value="'+data2.category_id+'" >'+data2.name+'</option>';
//                     $('#category_id').append(member)
//                 }else {
//                     var member='<option value="'+data2.category_id+'" >'+data2.name+'</option>';
//                     $('#category_id').append(member)
//                 }
//             })
//         }
//     })
// })
function discount() {
    $("#category_id").change(function(event) {
        var fanx = $(this).find("option:selected");
        if(fanx.val()==19) {
            window.location.href = "../operation_product/product.html"
        }
    });
}